package com.hubery.designpattern.design.chain;

import com.hubery.designpattern.design.chain.dto.ChainCtxDTO;
import com.hubery.designpattern.enums.ChainBizTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lxy
 * @date: 2023/2/27 23:56
 * @Description:
 */
@Slf4j
@Component
public class DefaultChainManageService extends ChainSupport {

    public void buildChain(ChainCtxDTO chainCtxDTO, ChainBizTypeEnum... typeEnums) {

        List<IChain> chainList = Arrays.stream(typeEnums).map(this::getChainService).collect(Collectors.toList());
        chain(chainList, chainCtxDTO);
    }

    public void chain(List<IChain> chainList, ChainCtxDTO dto) {

        for (var chain : chainList) {

            if (!chain.check(dto)) {
                continue;
            }

            chain.doWork(dto);
        }
    }
}
